package com.aw.baselib.util

import android.content.Context
import com.aw.baselib.base.AwBaseApplication

/**
 * Created by hzw
 */
object AwSpUtil {
    /**
     * 保存 int
     * @param xmlName xml文件名
     * @param key     要保存的 key
     * @param value   要保存的 value
     */
    fun saveInt(xmlName: String?, key: String?, value: Int) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.putInt(key, value)
        editor?.commit()
    }

    /**
     * 保存string数组
     * @param xmlName   xml文件名
     * @param key       要保存的 key
     * @param stringSet 要保存的 value
     */
    fun savaStringSet(xmlName: String?, key: String?, stringSet: Set<String?>?) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.putStringSet(key, stringSet)
        editor?.commit()
    }

    /**
     * 获取string数组
     * @param xmlName
     * @param key
     * @param stringSet
     * @return
     */
    fun getStringSet(
        xmlName: String?,
        key: String?,
        stringSet: Set<String?>?
    ): Set<String>? {
        val sharedPreferences =
            AwBaseApplication.instance?.applicationContext?.getSharedPreferences(
                xmlName,
                Context.MODE_PRIVATE
            )
        return sharedPreferences?.getStringSet(key, stringSet)
    }

    /**
     * 保存 String
     * @param xmlName xml文件名
     * @param key     要保存的 key
     * @param value   要保存的 value
     */
    fun saveString(xmlName: String?, key: String?, value: String?) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.putString(key, value)
        editor?.commit()
    }

    /**
     * 保存 boolean
     *
     * @param xmlName xml文件名
     * @param key     要保存的 key
     * @param value   要保存的 value
     */
    fun saveBoolean(xmlName: String?, key: String?, value: Boolean) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.putBoolean(key, value)
        editor?.commit()
    }

    /**
     * 保存 float
     *
     * @param xmlName xml文件名
     * @param key     要保存的 key
     * @param value   要保存的 value
     */
    fun saveFloat(xmlName: String?, key: String?, value: Float) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.putFloat(key, value)
        editor?.commit()
    }

    /**
     * 保存 long
     *
     * @param xmlName xml文件名
     * @param key     要保存的 key
     * @param value   要保存的 value
     */
    fun saveLong(xmlName: String?, key: String?, value: Long) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.putLong(key, value)
        editor?.commit()
    }

    /**
     * 保存 Set < String >
     *
     * @param xmlName xml文件名
     * @param key     要保存的 key
     * @param value   要保存的 value
     */
    fun saveSet(xmlName: String?, key: String?, value: Set<String?>?) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.putStringSet(key, value)
        editor?.commit()
    }

    /**
     * 清除当前xml 某一个key对应的值
     *
     * @param xmlName xml文件名
     * @param key     要清除的key
     */
    fun clearOne(xmlName: String?, key: String?) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.remove(key)
        editor?.commit()
    }

    /**
     * 清除xml中所有的值
     *
     * @param xmlName xml文件名
     */
    fun clearAll(xmlName: String?) {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        val editor = sharedPreferences?.edit()
        editor?.clear()
        editor?.commit()
    }

    /**
     * 获取int 值
     *
     * @param xmlName      xml文件名
     * @param key          要获取的key
     * @param defaultValue 默认值
     * @return
     */
    fun getInt(xmlName: String?, key: String?, defaultValue: Int): Int? {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        return sharedPreferences?.getInt(key, defaultValue)
    }

    /**
     * 获取 String 值
     *
     * @param xmlName      xml文件名
     * @param key          要获取的key
     * @param defaultValue 默认值
     * @return
     */
    fun getString(
        xmlName: String?,
        key: String?,
        defaultValue: String?
    ): String? {
        val sharedPreferences =
            AwBaseApplication.instance?.applicationContext?.getSharedPreferences(
                xmlName,
                Context.MODE_PRIVATE
            )
        return sharedPreferences?.getString(key, defaultValue)
    }

    /**
     * 获取Boolean 值
     *
     * @param xmlName      xml文件名
     * @param key          要获取的key
     * @param defaultValue 默认值
     * @return
     */
    fun getBoolean(
        xmlName: String?,
        key: String?,
        defaultValue: Boolean
    ): Boolean? {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        return sharedPreferences?.getBoolean(key, defaultValue)
    }

    /**
     * 获取long 值
     *
     * @param xmlName      xml文件名
     * @param key          要获取的key
     * @param defaultValue 默认值
     * @return
     */
    fun getLong(
        xmlName: String?,
        key: String?,
        defaultValue: Long
    ): Long? {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        return sharedPreferences?.getLong(key, defaultValue)
    }

    /**
     * 获取float 值
     *
     * @param xmlName      xml文件名
     * @param key          要获取的key
     * @param defaultValue 默认值
     * @return
     */
    fun getFloat(
        xmlName: String?,
        key: String?,
        defaultValue: Float
    ): Float? {
        val sharedPreferences =
            AwBaseApplication.instance?.getSharedPreferences(xmlName, Context.MODE_PRIVATE)
        return sharedPreferences?.getFloat(key, defaultValue)
    }
}